﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace HdipCricketClub.Models
{
    [MetadataType(typeof(ClubOtherFixtureDetailMetaData))]
    public partial class ClubOtherFixtureDetail
    {
        CricketClubEntities db = new CricketClubEntities();

        public void UpdateClubOtherFixtureDetails(FormCollection scorecard, Fixture fixture)
        {
            ClubOtherFixtureDetail currentClubOtherDetails = db.ClubOtherFixtureDetails.Where(cofd => cofd.fixture_id == fixture.fixture_id).Single();

            string byes = scorecard.GetValues("ClubOtherFixtureDetail.byes").Single();
            string leg_byes = scorecard.GetValues("ClubOtherFixtureDetail.leg_byes").Single();
            string wides = scorecard.GetValues("ClubOtherFixtureDetail.wides").Single();
            string no_balls = scorecard.GetValues("ClubOtherFixtureDetail.no_balls").Single();
            
            double clubTotalExtras = 0;

            if (byes != "")
            {
                currentClubOtherDetails.byes = double.Parse(byes);
                clubTotalExtras += (double)currentClubOtherDetails.byes;
            }

            if (leg_byes != "")
            {
                currentClubOtherDetails.leg_byes = double.Parse(leg_byes);
                clubTotalExtras += (double)currentClubOtherDetails.leg_byes;
            }

            if (wides != "")
            {
                currentClubOtherDetails.wides = double.Parse(wides);
                clubTotalExtras += (double)currentClubOtherDetails.wides;
            }

            if (no_balls != "")
            {
                currentClubOtherDetails.no_balls = double.Parse(no_balls);
                clubTotalExtras += (double)currentClubOtherDetails.no_balls;
            }
                        
            db.ObjectStateManager.ChangeObjectState(currentClubOtherDetails, EntityState.Modified);
            db.SaveChanges();
        }
                


    }

    public class ClubOtherFixtureDetailMetaData
    {
        [Required]
        [Display(Name = "Byes")]
        public double byes;

        [Required]
        [Display(Name = "Leg Byes")]
        public double leg_byes;

        [Required]
        [Display(Name = "Wides")]
        public double wides;

        [Required]
        [Display(Name = "No Balls")]
        public double no_balls;
    }
}